package com.longge;

import java.util.ArrayList;
import java.util.List;

public class LeafSimilar {

    /**
     * 叶子相似的树
     * @param root1
     * @param root2
     * @return
     */
    public boolean leafSimilar(TreeNode root1, TreeNode root2) {

        ArrayList<Integer> list1 = new ArrayList<>();
        ArrayList<Integer> list2 = new ArrayList<>();

        getLeafValues(root1,list1);
        getLeafValues(root2,list2);
        return list1.equals(list2);
    }

    private void getLeafValues(TreeNode root, List<Integer> list) {
        if (root==null) return;

        if (root.left==null&&root.right==null) list.add(root.val);
        getLeafValues(root.left,list);
        getLeafValues(root.right,list);
    }

}
